package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.SeckillOrderService;
import com.dongdongshop.service.SeckillService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("seckillController")
public class SeckillController {

    //开启日志
    Logger logger = LoggerFactory.getLogger(CartController.class);

    @Reference(check=false ,timeout=106666)
    private SeckillService seckillService;

    @Reference(check=false ,timeout=106666)
    private SeckillOrderService seckillOrderService;

    //跳转到商品秒杀页面
    @RequestMapping("toSeckillList")
    public String seckillList(Model model){
        List<TbSeckillGoods> list = seckillService.getSeckillGoodsInfo();
        model.addAttribute("list",list);
        return "seckill-index";
    }
    //根据id获取秒杀商品
    @RequestMapping("findSeckillGoods/{id}")
    public String findSeckillGoods(@PathVariable("id") Long id, Model model){
        TbSeckillGoods seckillGoods = seckillService.findSeckillGoods(id);
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";
    }

    //提交订单，进行抢购
    @RequestMapping("submitOrder")
    @ResponseBody
    public Result submitOrder(Long seckillId){
        try {
            logger.info("提交订单方法开始运行{}");
            //获取登录用户
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            if (null == user){
                return Result.response(ResultEnum.UNAUTH).setData("请先登录");
            }
            //使用dubbo提交订单
            seckillOrderService.submitOrder(seckillId,user.getId());

            return Result.response(ResultEnum.SUCCESS).setData(seckillId);
        }catch (RuntimeException e){
            //补货service里抛出的异常
            return Result.response(ResultEnum.ERROR).setData(e.getMessage());
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return Result.response(ResultEnum.ERROR).setData("服务器异常");
        }
    }
    //查询订单是否生成
    @RequestMapping("findOrderFromRedis")
    @ResponseBody
    public Result findOrderFromRedis(Long seckillId){
        try {
            logger.info("查询订单是否生成方法开始运行{}");
            //获取登录用户
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            if (null == user){
                return Result.response(ResultEnum.UNAUTH).setData("请先登录");
            }
            TbSeckillOrder seckillOrder = seckillOrderService.findOrderFromRedis(seckillId,user.getId());
            if (seckillOrder != null) {
                return Result.response(ResultEnum.SUCCESS);
            }
            return Result.response(ResultEnum.FAIL);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            //补货service里抛出的异常
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("payList")
    public String payList(){
        return "payList";
    }


}
